package localdatetimedemo;

import javax.swing.text.DateFormatter;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class TestDate {
    public static void main(String[] args) {
        LocalDateTime currentDate = LocalDateTime.now();

        LocalDateTime firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth());

        LocalDateTime lastDayOfMOnth = currentDate.with(TemporalAdjusters.lastDayOfMonth());

        System.out.println(firstDayOfMonth);
        System.out.println(lastDayOfMOnth);
    }

    public static void main7(String[] args) {
           //获取当前所在日期的周一的日期
        LocalDateTime dateTime = LocalDateTime.now();

        LocalDateTime firstDayOfWeek = dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

        //获取当前所在日期的周日
        LocalDateTime lastDayOfWeek = dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));

        System.out.println(firstDayOfWeek);
        System.out.println(lastDayOfWeek);
    }
    public static void main6(String[] args) {
        //演示localDateTime类增加和减少日期
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("当前的日期是："+ dateTime);

        LocalDateTime newPlusDay = dateTime.plusDays(1);
        //增加一天之后的日期
        System.out.println("增加一天之后的日期是："+newPlusDay);

        //增加一个月之后的日期
        LocalDateTime newPlusMonth = dateTime.plusMonths(1);
        System.out.println("增加一个月之后的日期是：" + newPlusMonth);

        //增加一年之后的日期
        LocalDateTime newPlusYear = dateTime.plusYears(1);
        System.out.println("增加一年之后的日期是" + newPlusYear);

        //增加一个小时之后的日期
        LocalDateTime newPlusHour = dateTime.plusHours(1);
        System.out.println("增加一个小时之后的日期是:" +  newPlusHour);


        //增加一周之后的日期
        LocalDateTime newPulsWeek = dateTime.plusWeeks(1);
        System.out.println("增加一周之后的日期是：" + newPulsWeek);

        //减少一天之后的日期
        LocalDateTime newMinDay = dateTime.minusDays(1);
        System.out.println("减少一天之后的日期是：" + newMinDay);

    }
    public static void main5(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("当前的日期为：" + localDateTime);

        //获取当前的日期是本周的第几天
        int dayOfWeek = localDateTime.getDayOfWeek().getValue();
        //获取当前的日期是这一年的第几天
        int dayOfYear = localDateTime.getDayOfYear();

        System.out.println("本周第几： " + dayOfWeek);
        System.out.println("本月第几：" + localDateTime.getDayOfMonth());
        System.out.println("本年第几：" + dayOfYear);
    }
    public static void main4(String[] args) {
        //根据给定的字符串和日期格式对象构造一个LocalDateTime对象
        String date = "2026-10-01 10:30:00";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(date,dateTimeFormatter);
        System.out.println("当前日期为：" + localDateTime);
    }
    public static void main3(String[] args) {
        //获取LocalDateTime对象中的年月日，时分秒
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int month = localDateTime.getMonth().getValue();
        int day = localDateTime.getDayOfMonth();
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        System.out.println("year = " + year);
        System.out.println("month = " + month);
        System.out.println("day = " + day);
        System.out.println("hour = " + hour);
        System.out.println("minute = " + minute);
        System.out.println("second = " + second);
    }
    public static void main2(String[] args) {
        //获取现在的时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        //根据给定的年月日构造localDateTime对象
        LocalDateTime localDateTime1 = LocalDateTime.of(2024,12,21,8,30);
        System.out.println(localDateTime1);

        //从字符串中解析出一个LocalDateTime对象
        LocalDateTime parsed = LocalDateTime.parse(" 2025-07-03T10:15:30");
        System.out.println(parsed);
    }
    //演示Date类的构造方法
    public static void main1(String[] args) {
        //Date类已经过时，这里仅作了解即可
        //123代表从1900年开始往上加123年，0代表1月份，1代表2月份，以此类推
        Date date = new Date(123,0,29);
        System.out.println(date);
    }
}
